package com.zhangln.mountbuzhou.dao.impl;

import com.zhangln.mountbuzhou.core.RegContent;
import com.zhangln.mountbuzhou.entity.TbConnectionLogEntity;
import com.zhangln.mountbuzhou.mapper.TbConnectionLogMapper;
import com.zhangln.mountbuzhou.dao.TbConnectionLogDao;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 客户端在线情况日志表 服务实现类
 * </p>
 *
 * @author sherry
 * @since 2020-04-27
 */
@Service
public class TbConnectionLogDaoImpl extends ServiceImpl<TbConnectionLogMapper, TbConnectionLogEntity> implements TbConnectionLogDao {

    @Override
    public void saveConnectionLog(String channelId) {
        TbConnectionLogEntity tbConnectionLogEntity = TbConnectionLogEntity
                .builder()
                .channelId(channelId)
                .actionType(1)
                .note("连接")
                .area("-1")
                .orgCode("-1")
                .userId("-1")
                .revision(0)
                .createdTime(LocalDateTime.now())
                .updatedTime(LocalDateTime.now())
                .createdBy("-1")
                .updatedBy("-1")
                .build();
        this.save(tbConnectionLogEntity);
    }

    @Override
    public void saveConnectionRefusedLog(String channelId) {
//        查询出此连接最近一次的连接日志
        Long maxId = this.baseMapper.getMaxTbConnectionLogIdByChannelId(channelId);
        if (maxId == null) {
            TbConnectionLogEntity tbConnectionLogEntity = TbConnectionLogEntity
                    .builder()
                    .channelId(channelId)
                    .actionType(3)
                    .note("注册失败")
                    .area("-1")
                    .orgCode("-1")
                    .userId("-1")
                    .revision(0)
                    .createdTime(LocalDateTime.now())
                    .updatedTime(LocalDateTime.now())
                    .createdBy("-1")
                    .updatedBy("-1")
                    .build();
            this.save(tbConnectionLogEntity);
        } else {
            TbConnectionLogEntity tbConnectionLogEntity = this.getById(maxId);
            tbConnectionLogEntity.setActionType(3);
            tbConnectionLogEntity.setNote("注册失败");
            tbConnectionLogEntity.setUpdatedTime(LocalDateTime.now());
            tbConnectionLogEntity.setId(null);
            TbConnectionLogEntity tmp = new TbConnectionLogEntity();
            BeanUtils.copyProperties(tbConnectionLogEntity, tmp);
            this.save(tmp);
        }
    }

    @Override
    public void saveConnectionSuccLog(String channelId, RegContent regContent) {
        Long maxId = this.baseMapper.getMaxTbConnectionLogIdByChannelId(channelId);
        if (maxId == null) {
            TbConnectionLogEntity tbConnectionLogEntity = TbConnectionLogEntity
                    .builder()
                    .channelId(channelId)
                    .actionType(2)
                    .note("注册成功")
                    .area(StringUtils.isEmpty(regContent.getArea()) ? "-1" : regContent.getArea())
                    .orgCode(StringUtils.isEmpty(regContent.getOrgCode()) ? "-1" : regContent.getOrgCode())
                    .userId(StringUtils.isEmpty(regContent.getUserId()) ? "-1" : regContent.getUserId())
                    .revision(0)
                    .createdTime(LocalDateTime.now())
                    .updatedTime(LocalDateTime.now())
                    .createdBy("-1")
                    .updatedBy("-1")
                    .build();
            this.save(tbConnectionLogEntity);
        } else {
            TbConnectionLogEntity tbConnectionLogEntity = this.getById(maxId);
            tbConnectionLogEntity.setActionType(2);
            tbConnectionLogEntity.setNote("注册成功");
            tbConnectionLogEntity.setArea(StringUtils.isEmpty(regContent.getArea()) ? "-1" : regContent.getArea());
            tbConnectionLogEntity.setOrgCode(StringUtils.isEmpty(regContent.getOrgCode()) ? "-1" : regContent.getOrgCode());
            tbConnectionLogEntity.setUserId(StringUtils.isEmpty(regContent.getUserId()) ? "-1" : regContent.getUserId());
            tbConnectionLogEntity.setUpdatedTime(LocalDateTime.now());
            tbConnectionLogEntity.setId(null);
            TbConnectionLogEntity tmp = new TbConnectionLogEntity();
            BeanUtils.copyProperties(tbConnectionLogEntity, tmp);
            this.save(tmp);
        }
    }

    @Override
    public void savePingLog(String channelId) {
        Long maxId = this.baseMapper.getMaxTbConnectionLogIdByChannelId(channelId);
        if (maxId == null) {
            TbConnectionLogEntity tbConnectionLogEntity = TbConnectionLogEntity
                    .builder()
                    .channelId(channelId)
                    .actionType(4)
                    .note("心跳包")
                    .area("-1")
                    .orgCode("-1")
                    .userId("-1")
                    .revision(0)
                    .createdTime(LocalDateTime.now())
                    .updatedTime(LocalDateTime.now())
                    .createdBy("-1")
                    .updatedBy("-1")
                    .build();
            this.save(tbConnectionLogEntity);
        } else {
//            查询他的心跳包信息
            TbConnectionLogEntity tbConnectionLogEntity2 = this.baseMapper.getLastHeartPing(channelId);
            if (tbConnectionLogEntity2 == null) {
                TbConnectionLogEntity tbConnectionLogEntity = this.getById(maxId);
                tbConnectionLogEntity.setActionType(4);
                tbConnectionLogEntity.setNote("心跳包");
                tbConnectionLogEntity.setUpdatedTime(LocalDateTime.now());
                tbConnectionLogEntity.setId(null);
                TbConnectionLogEntity tmp = new TbConnectionLogEntity();
                BeanUtils.copyProperties(tbConnectionLogEntity, tmp);
                this.save(tmp);
            } else {
                tbConnectionLogEntity2.setUpdatedTime(LocalDateTime.now());
                this.baseMapper.updateUpdatetimeById(tbConnectionLogEntity2.getId(), tbConnectionLogEntity2.getUpdatedTime());
            }

        }
    }

    @Override
    public void saveHandlerRemovedLog(String channelId, String msg) {
        Long maxId = this.baseMapper.getMaxTbConnectionLogIdByChannelId(channelId);
        if (maxId == null) {
            TbConnectionLogEntity tbConnectionLogEntity = TbConnectionLogEntity
                    .builder()
                    .channelId(channelId)
                    .actionType(5)
                    .note("客户端掉线")
                    .area("-1")
                    .orgCode("-1")
                    .userId("-1")
                    .revision(0)
                    .createdTime(LocalDateTime.now())
                    .updatedTime(LocalDateTime.now())
                    .createdBy("-1")
                    .updatedBy("-1")
                    .build();
            this.save(tbConnectionLogEntity);
        } else {
            TbConnectionLogEntity tbConnectionLogEntity = this.getById(maxId);
            tbConnectionLogEntity.setActionType(5);
            tbConnectionLogEntity.setNote("客户端掉线");
            tbConnectionLogEntity.setUpdatedTime(LocalDateTime.now());
            tbConnectionLogEntity.setId(null);
            TbConnectionLogEntity tmp = new TbConnectionLogEntity();
            BeanUtils.copyProperties(tbConnectionLogEntity, tmp);
            this.save(tmp);
        }
    }

    @Override
    public void truncate() {
        this.baseMapper.truncate();
    }
}